package com.sun.config;/**
 * @author sun
 * @date 2022/1/5 - 15:29
 */

import com.sun.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;

import java.util.ArrayList;

/**
 * @description: 授权服务器得配置
 * @author 孙
 * @date 2022/1/5 15:29
 * @version 1.0
 */
@Configuration
@EnableAuthorizationServer//开启授权得服务
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    AuthenticationManager authenticationManager;

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    @Qualifier("JwtTokenStore")
    private TokenStore tokenStore;
    @Autowired
    private JwtAccessTokenConverter jwtAccessTokenConverter;
    @Autowired
    private JwtTokenEnhancer jwtTokenEnhancer;



    /**
     * 添加这个配置之后即可使用密码模式
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        //配置JWT内容增强器
         TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
         ArrayList<TokenEnhancer> delegates = new ArrayList<>();
         delegates.add(jwtTokenEnhancer);
         delegates.add(jwtAccessTokenConverter);
        enhancerChain.setTokenEnhancers(delegates);

        endpoints.authenticationManager(authenticationManager)
                .userDetailsService(userService)
                //配置存储令牌策略
                .tokenStore(tokenStore)
                .accessTokenConverter(jwtAccessTokenConverter)
                .tokenEnhancer(enhancerChain);
    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        /*
        这里的客户端指的是我们的应用，也是就到授权服务器这里获取授权码的应用，这里为了方便直接另其在
        内存中生成了。
         */
        clients.inMemory()
                // 客户端id
                .withClient("client")
                // 密钥
                .secret(passwordEncoder.encode("112233"))
                //配置有效时间
                .accessTokenValiditySeconds(3600)
                //配置刷新token有效时间
                .refreshTokenValiditySeconds(864000)
                // 重定向地址（通过这个地址我们可以拿到授权码(地址栏)）
                .redirectUris("http://localhost:8081/login")
                //自动授权
                .autoApprove(true)
                // 授权范围
                .scopes("all")
                /*
                授权类型
                authorization_code:授权码模式
                password: 密码模式
                 */
                .authorizedGrantTypes("authorization_code","password","refresh_token");

    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //获取密钥需要身份验证，使用单点登录时必须认证
        security.tokenKeyAccess("isAuthenticated()");
    }


}
